import { parse } from 'mathjs'

export const analyzeDependencies = (expression) => {
  try {
    const ast = parse(expression)
    const dependencies = new Set()

    ast.traverse((node) => {
      if (node.type === 'SymbolNode' && node.name.match(/^f\d+/)) {
        dependencies.add(node.name)
      }
    })

    return Array.from(dependencies)
  }
  catch (error) {
    console.error('Dependency analysis failed:', error)
    return []
  }
}

export const validateFormula = (expression) => {
  try {
    parse(expression)
    return { valid: true }
  }
  catch (error) {
    return {
      valid: false,
      message: error.toString(),
    }
  }
}
